package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.GoodsEntity;
import com.softlab.retailwholesalesystem.entity.OrderEntity;
import com.softlab.retailwholesalesystem.entity.OrderGoodsMapEntity;
import com.softlab.retailwholesalesystem.mapper.OrderGoodsMapMapper;
import com.softlab.retailwholesalesystem.service.IGoodsService;
import com.softlab.retailwholesalesystem.service.IOrderGoodsMapService;
import com.softlab.retailwholesalesystem.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class OrderGoodsMapService extends ServiceImpl<OrderGoodsMapMapper, OrderGoodsMapEntity> implements IOrderGoodsMapService {

    @Autowired
    OrderGoodsMapMapper mapper;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IOrderService orderService;

    @Override
    public List<OrderGoodsMapEntity> getMap(OrderGoodsMapEntity map) {
        QueryWrapper<OrderGoodsMapEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("u.is_delete",0);
        wrapper.eq("u.order_id",map.getOrderId());
        return mapper.getMap(wrapper);
    }

    @Override
    public void setSinglePriceByType(OrderGoodsMapEntity map){
        if (map.getSinglePrice() == null || map.getSinglePrice() == 0){
            OrderEntity order = orderService.getById(map.getOrderId());
            switch (order.getOrderType()){
                case "Q":
                case "A":
                    map.setSinglePrice(goodsService.getById(map.getGoodsId()).getaPrice());
                    break;
                case "B":
                    map.setSinglePrice(goodsService.getById(map.getGoodsId()).getbPrice());
                    break;
                case "C":
                    map.setSinglePrice(goodsService.getById(map.getGoodsId()).getcPrice());
                    break;
                default:
                    map.setSinglePrice(goodsService.getById(map.getGoodsId()).getaPrice());
                    break;
            }
        }

        mapper.updateById(map);

    }


    @Override
    public void init(){
        List<OrderGoodsMapEntity> orderGoodsMapEntities = mapper.selectList(null);
        for (OrderGoodsMapEntity e :
                orderGoodsMapEntities) {
            setSinglePriceByType(e);
            mapper.updateById(e);

        }
    }

    @Override
    public void saveQuik(Long orderId, List<OrderGoodsMapEntity> map) {
        if (map == null)
            return;
        for (OrderGoodsMapEntity e:
             map) {
            e.setOrderId(orderId);
            mapper.insert(e);
            setSinglePriceByType(e);
        }

    }


}
